const defaultReceiverInfo = {
    orderId:null,
    receiverName:null,
    receiverPhone:null,
    receiverPostCode:null,
    receiverDetailAddress:null,
    receiverProvince:null,
    receiverCity:null,
    receiverRegion:null,
    status:null
  };
  

new Vue({
    el: '#app',
    data: function () {
        return {
            visible: false,
            isCollapse: false,
            opened: false,
            isActive: false,
            id: null,
        order: {},
        receiverDialogVisible:false,
        receiverInfo:Object.assign({},defaultReceiverInfo),
        moneyDialogVisible:false,
        moneyInfo:{orderId:null, freightAmount:0, discountAmount:0,status:null},
        closeDialogVisible:false,
        closeInfo:{note:null,id:null},
        logisticsDialogVisible:false
        }
    },
    created() {
        this.id = this.list = this.$route.query.id;
        getOrderDetail(this.id).then(response => {
          this.order = response.data;
        });
      },
      filters: {
        formatNull(value) {
          if(value===undefined||value===null||value===''){
            return '暂无';
          }else{
            return value;
          }
        },
        formatLongText(value) {
          if(value===undefined||value===null||value===''){
            return '暂无';
          }else if(value.length>8){
            return value.substr(0, 8) + '...';
          }else{
            return value;
          }
        },
        formatAddress(order) {
          let str = order.receiverProvince;
          if (order.receiverCity != null) {
            str += "  " + order.receiverCity;
          }
          str += "  " + order.receiverRegion;
          str += "  " + order.receiverDetailAddress;
          return str;
        },
        formatStatus(value) {
          if (value === 1) {
            return '待发货';
          } else if (value === 2) {
            return '已发货';
          } else if (value === 3) {
            return '已完成';
          } else if (value === 4) {
            return '已关闭';
          } else if (value === 5) {
            return '无效订单';
          } else {
            return '待付款';
          }
        },
        formatPayStatus(value) {
          if (value === 0) {
            return '未支付';
          } else if(value===4){
            return '已退款';
          }else{
            return '已支付';
          }
        },
        formatDeliverStatus(value) {
          if (value === 0||value === 1) {
            return '未发货';
          } else {
            return '已发货';
          }
        },
      },
    methods: {
        handleOpen(key, keyPath) {
            console.log(key, keyPath);
        },
        handleClose(key, keyPath) {
            console.log(key, keyPath);
        },
        sidebarHide(){
            $(".sidebar-container").addClass("sidebar-hide");
            alert("sidebarHide");
        },
        sidebarOpen(){
            $(".sidebar-container").removeClass("sidebar-hide");
            alert("sidebarOpen");
        },
        toggleSideBar(){
            alert("togglesidebar");
        },
        sideBar(){
            alert("sidebar");
        },
        toggleClick(){
            this.isActive = !this.isActive;
            this.isCollapse=!this.isCollapse;
        },
        handleCommand(command) {
            window.location.href="/home";
        },
        logout(){

        },
        onSelectRegion(data){
            this.receiverInfo.receiverProvince=data.province.value;
            this.receiverInfo.receiverCity=data.city.value;
            this.receiverInfo.receiverRegion=data.area.value;
          },
          formatTime(time) {
            if (time == null || time === '') {
              return '';
            }
            let date = new Date(time);
            return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
          },
          formatStepStatus(value) {
            if (value === 1) {
              //待发货
              return 2;
            } else if (value === 2) {
              //已发货
              return 3;
            } else if (value === 3) {
              //已完成
              return 4;
            }else {
              //待付款、已关闭、无限订单
              return 1;
            }
          },
          showUpdateReceiverDialog(){
            this.receiverDialogVisible=true;
            this.receiverInfo={
              orderId:this.order.id,
              receiverName:this.order.receiverName,
              receiverPhone:this.order.receiverPhone,
              receiverPostCode:this.order.receiverPostCode,
              receiverDetailAddress:this.order.receiverDetailAddress,
              receiverProvince:this.order.receiverProvince,
              receiverCity:this.order.receiverCity,
              receiverRegion:this.order.receiverRegion,
              status:this.order.status
            }
          },
          handleUpdateReceiverInfo(){
            this.$confirm('是否要修改收货信息?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              updateReceiverInfo(this.receiverInfo).then(response=>{
                this.receiverDialogVisible=false;
                this.$message({
                  type: 'success',
                  message: '修改成功!'
                });
                getOrderDetail(this.id).then(response => {
                  this.order = response.data;
                });
              });
            });
          },
          showCloseOrderDialog(){
            this.closeDialogVisible=true;
            this.closeInfo.note=null;
            this.closeInfo.id=this.id;
          },
          handleCloseOrder(){
            this.$confirm('是否要关闭?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
                let params = new URLSearchParams();
                params.append("ids",[this.closeInfo.id]);
                params.append("note",this.closeInfo.note);
                closeOrder(params).then(response=>{
                  this.closeDialogVisible=false;
                  this.$message({
                    type: 'success',
                    message: '订单关闭成功!'
                  });
                  getOrderDetail(this.id).then(response => {
                    this.order = response.data;
                  });
                });
            });
          },
          handleDeleteOrder(){
            this.$confirm('是否要进行该删除操作?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              let params = new URLSearchParams();
              params.append("ids",[this.id]);
              deleteOrder(params).then(response=>{
                this.$message({
                  message: '删除成功！',
                  type: 'success',
                  duration: 1000
                });
                this.$router.back();
              });
            })
          },
    }
})

function closeOrder(params) {
    return request({
      url:'/#',
      method:'post',
      params:params
    })
  }
 function deleteOrder(params) {
    return request({
      url:'/#',
      method:'post',
      params:params
    })
  }
   function getOrderDetail(id) {
    return request({
      url:'/#',
      method:'get'
    });
  }
  
  function updateReceiverInfo(data) {
    return request({
      url:'/#',
      method:'post',
      data:data
    });
  }
  function formatDate(date, fmt) {
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds()
    };
    for (let k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        let str = o[k] + '';
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
      }
    }
    return fmt;
  }